import Vue from 'vue'
import Clipboard from 'clipboard'
import { isString } from './type-check'

function clipboardSuccess() {
    Vue.prototype.$message(
        {
            message: '复制成功',
            type: 'success',
            duration: 1500
        }
    )
}
    

function clipboardError() {
    Vue.prototype.$message(
        {
            message: '复制失败',
            type: 'error'
        }
    )
}

export default function handleClipboard(event, text) {
    const formatText = isString(text) ? text : JSON.stringify(text)
    
    const clipboard = new Clipboard(
        event.target, 
        {
            text: () => formatText
        }
    )
    clipboard.on('success', () => {
        clipboardSuccess()
        clipboard.destroy()
    })
    clipboard.on('error', () => {
        clipboardError()
        clipboard.destroy()
    })
    clipboard.onClick(event)
}
    